﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace AB
{
    public class TrialBlock
    {
        public int NUM_TRIALS = 5;
        public String subjectName;
        public String blockType;

        public int trialNumber = 0;
        public Trial[] trials;

        public Trial currentTrial;

        public DateTime startTime;
        public DateTime endTime;

        public TrialBlock() {
            setNumTrials(NUM_TRIALS);
        }

        public Boolean nextTrial()
        {
            if (trialNumber < NUM_TRIALS)
            {
                //Trial newTrial = new Trial(true, (((trialNumber % 4)+1)*2));
                //trials[trialNumber++] = newTrial;
                //currentTrial = newTrial;
                currentTrial = trials[trialNumber++];
                return true;
            }
            else
            {
                return false;
            }
        }
        public Trial[] getTrials()
        {
            return trials;
        }

        public void getResults()
        {
            //String path = Directory.GetCurrentDirectory() + "\\" + subjectName + "_" + blockType +".csv";
            //String path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop).ToString() + "\\" + subjectName + "_" + blockType + ".csv";

            String desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\AB_Results";

            System.IO.Directory.CreateDirectory(desktopDir);

            String path = desktopDir + "\\" + subjectName + "_" + blockType + ".csv";


            Console.WriteLine(path);
            String delimiter = ",";  
	 
	        String[] output = new String[8];
            StringBuilder sb = new StringBuilder();

            TimeSpan duration = endTime - startTime;
            output[0] = "Subject Name";
            output[1] = "Block Type";
            output[2] = "Block time";
            sb.AppendLine(String.Join(delimiter, output));

            output[0] = subjectName;
            output[1] = blockType;
            output[2] = duration.Minutes.ToString() + ":" + duration.Seconds.ToString() + "." + duration.Milliseconds.ToString();
            sb.AppendLine(String.Join(delimiter, output));

            output[0] = "Trial";
            output[1] = "Target";
            output[2] = "Target Guess";
            output[3] = "Target_Identified";
            output[4] = "Probe_Present";
            output[5] = "Probe Identified";
            output[6] = "Probe_Position";
            output[7] = "Trial Time";

            
            sb.AppendLine(String.Join(delimiter,output));

            for (int i = 1; i <= NUM_TRIALS; i++)
            {
                Trial t = trials[i-1];
                output[0] = "T" + i;
                output[1] = t.target;
                output[2] = t.targetGuess;
                output[3] = t.correctTarget.ToString();
                output[4] = t.getProbePresent().ToString();
                output[5] = t.correctProbe.ToString();
                output[6] = t.probePosition.ToString();
                output[7] = t.getTrialTime().Seconds.ToString() +"." + t.getTrialTime().Milliseconds.ToString();
                sb.AppendLine(String.Join(delimiter, output));
            }

            
	        File.WriteAllText(path, sb.ToString()); 
        }

        public int getNumTrials()
        {
            return NUM_TRIALS;
        }

        public void setNumTrials(int num)
        {
            NUM_TRIALS = num;
            Random random = new Random();
            trials = new Trial[NUM_TRIALS];
            Trial t;
            for (int i = 0; i <NUM_TRIALS; i++)
            {
                t = new Trial((i%8>3),(((i % 4)+1)*2));
                trials[i] = t;
            }
            trials = Shuffle<Trial>(trials);
        }

        public void setSubjectName(String name)
        {
            subjectName = name;
        }


        private static T[] Shuffle<T>(T[] OriginalArray)
        {
            var matrix = new SortedList();
            var r = new Random();

            for (var x = 0; x <= OriginalArray.GetUpperBound(0); x++)
            {
                var i = r.Next();

                while (matrix.ContainsKey(i)) { i = r.Next(); }

                matrix.Add(i, OriginalArray[x]);
            }

            var OutputArray = new T[OriginalArray.Length];

            matrix.Values.CopyTo(OutputArray, 0);

            return OutputArray;
        }

    }
}
